%%% This is an Euler equation system for solving k and N.

function val = FOC_fossil(x)

global parms i Sf kf Nf cf sigmaf T0 lambdaf
% Note that parms is a 1x14 vector with elements:
%
% parms = [delta A Sbar alpha0 alpha1 alpha2 alpha3 Gamma1 alpha psi beta gamma Q0 popgr];
%           1    2   3      4      5    6     7       8      9    10  11    12  13  14  

delta = parms(1);
A = parms(2);
Sbar = parms(3);
alpha0 = parms(4);
alpha1 = parms(5);
alpha2 = parms(6);
alpha3 = parms(7);
popgr = parms(14);

k = x(1);
N = x(2);

Q = (1+popgr)^(T0-i+1);
S = Sf(i-1)-Q*A*k;
    
g =  alpha0 + alpha1./(Sbar-alpha2./(alpha3+N)-S);
gdN = -alpha1.*alpha2./((Sbar-S).*(alpha3+N)-alpha2).^2;

val(1) = 1-g+sigmaf(i)*Q-delta/A+gdN*k;
val(2) = A*k*(1-g)+(1-delta)*k+N-cf(i)-kf(i-1)-Nf(i-1);







